﻿using Yxinmt.Interface;
using Yxinmt.Utility;
using System;
using System.Collections.Generic;

namespace Yxinmt.Controls
{
    public class Button : BaseControl, IControl
    {
        private System.Windows.Controls.Button _Button { get; set; }

        public Button(Dictionary<string, object> property, IComponent component)
        {
            Property = property;
            _Component = component;
            _Button = new System.Windows.Controls.Button();
            this.Element = _Button;

            InitSet();
            SetProperty();
            SetControlProperty();
        }

        private void SetProperty()
        {
            this.SetDefaultValue("IsClick", true);
        }

        protected override void SetProperty(string name, object value)
        {
            System.Windows.Controls.Button c = _Button;
            switch (name)
            {
                case "name": c.Name = string.Concat("btn_", (string)value); break;
                case "Text": c.Content = (string)value; break;
                case "Width":
                case "ControlWidth": c.Width = Convert.ToDouble(value); break;
                case "Height": c.Height = Convert.ToDouble(value); break;
                case "Margin": c.Margin = this.GetThickness((string)value); break;
                case "Padding": c.Padding = this.GetThickness((string)value); break;
                case "Visible": c.Visibility = (bool)value ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed; break;
                case "ZIndex": SetZIndex(Convert.ToInt32(value)); break;
                case "CanvasLocation": SetCanvasLocation((string)value); break;
                case "CanvasZIndex": SetCanvasZIndex(Convert.ToInt32(value)); break;
                case "BackColor": c.Background = GetColor((string)value); SetFactoryProperty("Background", c.Background); break;
                case "Dock": System.Windows.Controls.DockPanel.SetDock(c, this.GetDock((string)value)); break;
            }
        }

        public override void SetValue(object value)
        {
            _Button.Content = value;
        }

        public override object GetValue()
        {
            return _Button.Content;
        }

        /// <summary>
        /// 事件加载
        /// </summary>
        public override void EventLoad()
        {
            bool isClick = this.Property.GetValue<bool>("IsClick");
            if (isClick) this._Button.Click += _Button_Click;
        }

        void _Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                if (ClickAction != null) ClickAction(sender, e, this);
                else this._Window.EventInvoke(sender, e, this);
            }
            catch (Exception ex)
            {
                LoggerProxy.Exception(Name, "_Button_Click", ex);
            }
        }
    }
}
